$().ready(function() {
	$('.summernote').summernote({
		height: 500,
		width: 750,
		lang : 'zh-CN',
		callbacks: {
			onImageUpload: function(files, editor, $editable) {
				console.log("onImageUpload");
				sendFile(files);
			}
		}
	});
	var content = $("#content").val();

	$('#content_sn').summernote('code', content);
	validateRule();
});

$.validator.setDefaults({
	submitHandler : function() {
		update();
	}
});
function update() {
	$.ajax({
		cache : true,
		type : "POST",
		url : "/system/dlNews/update",
		data : $('#signupForm').serialize(),// 你的formid
		async : false,
		error : function(request) {
			parent.layer.alert("Connection error");
		},
		success : function(data) {
			if (data.code == 0) {
				parent.layer.msg("操作成功");
				parent.reLoad();
				var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
				parent.layer.close(index);
			} else {
				parent.layer.alert(data.msg)
			}

		}
	});

}
function fileUpload(id, type){
	var photo=$("#"+id).val();
	if(photo!=null && photo!=""){
		// 构建数据
		var data = new FormData()
		data.append('type', type)
		data.append('name', $('#'+id).val())
		data.append('file', $('#'+id)[0].files[0]) // file 对象
		$.ajax({
			cache : false,
			type : "POST",
			url : "/upload",
			data :data,
			processData: false,//数据不被转换为字符串
			contentType: false,//上传文件时使用，避免 JQuery 对其操作
			async : false,
			dataType:"json",
			error : function(request) {
			},
			success : function(data) {
				if (data.code == 0) {
					photo = data.path;
					$("input[name='"+id+"']").val(data.newFileName);
					console.log(photo);
					$("#show"+id).attr("src",photo);
				}else{
					parent.layer.alert(data.msg)
				}
			}
		});
	}
}
function validateRule() {
	var icon = "<i class='fa fa-times-circle'></i> ";
	$("#signupForm").validate({
		rules : {
			name : {
				required : true
			}
		},
		messages : {
			name : {
				required : icon + "请输入名字"
			}
		}
	})
}